<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增字典' : '编辑字典'" width="510px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="字典名称" prop="key" label-width="90px">
        <el-input v-model="form.label" style="width: 360px;"/>
      </el-form-item>
      <el-form-item label="字典编码" prop="key" label-width="90px">
        <el-input v-model="form.dict_code" style="width: 360px;"/>
      </el-form-item>
      <el-form-item label="字典值" prop="value" label-width="90px">
        <el-input v-model="form.dict_val" style="width: 360px;"/>
      </el-form-item>
      <el-form-item label="上级字典" label-width="90px">
        <treeselect
          v-model="form.parent_code"
          :options="dicts"
          :normalizer="normalizer"
          :max-height="300"
          style="width: 360px;"
          placeholder="请选择父级字典"
          @select="dictParentChange($event)"/>
      </el-form-item>
      <el-form-item label="是否可用" label-width="90px">
        <el-radio-group v-model="form.is_enabled">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" label-width="90px">
        <el-input-number v-model="form.sort_no_two" :min="0" controls-position="right" style="width: 360px;"></el-input-number>
      </el-form-item>
      <el-form-item style="margin-bottom: 0px;" label="描述" label-width="90px">
        <el-input v-model="form.remark" style="width: 360px;" rows="5" type="textarea"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="danger" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit, getDictDetail } from '@/api/dictDetail'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { Notification } from 'element-ui'

export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    dictId: {
      type: String,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      currentData: null,
      dicts: [],
      loading: false, dialog: false,
      form: {
        is_enabled: 1,
        parent_id: '',
        dict_id: this.dictId,
        dict_detail_id: '',
        sort_no_two: 0
      },
      rules: {
        label: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        dict_code: [
          { required: true, message: '请输入字典编码', trigger: 'blur' }
        ],
        dict_val: [
          { required: true, message: '请输入字典值', trigger: 'blur' }
        ]
      },
      normalizer(node) {
        // 去掉children=[]的children属性
        if (node.children == null || node.children.length === 0) {
          delete node.children
        }
        return {
          id: node.dict_code,
          label: node.label,
          children: node.children
        }
      }

    }
  },
  methods: {
    getDictDetail() {
      if (this.currentData) {
        const dictParams = { parent_code: this.currentData.dict_code }
        getDictDetail(dictParams).then(res => {
          if (res.code === 200) {
            this.dicts = []
            this.currentData.children = res.data.list
            this.dicts.push(this.currentData)
          }
        }).catch(err => {
          console.log(err)
        })
      }
    },
    dictParentChange(val) {
      console.log(val)
      this.form.parent_id = val.dict_detail_id
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.form.dict_id = this.dictId
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      add(this.form).then(res => {
        if (res.code === 200) {
          this.resetForm()
          this.$message({
            showClose: true,
            type: 'success',
            message: '添加成功!',
            duration: 2500
          })
          this.$emit('getDictDetail')
          this.$parent.$parent.getDictDetail()
        } else {
          Notification.error({
            title: '错误',
            message: '发布失败'
          })
        }
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      edit(this.form.id, this.form).then(res => {
        this.resetForm()
        this.$message({
          showClose: true,
          type: 'success',
          message: '修改成功!',
          duration: 2500
        })
        this.loading = false
        this.$emit('getDictDetail')
        this.sup_this.getDictDetail()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        is_enabled: 1,
        parent_id: '',
        dict_id: this.dictId,
        dict_detail_id: '',
        parent_code: '',
        sort_no_two: 0
      }
    }
  }
}
</script>

<style scoped>

</style>
